﻿using UnityEngine;
using Spine.Unity;
using UnityEngine.UI;

public class UnitDisplay : MonoBehaviour
{
    public UnitBase m_Unit;
    public BoardDisplay m_BoardDisplay;
    MeshRenderer m_MeshRenderer;
    UnitAnimation m_UnitAnimation;
    Text m_AtkText;
    Text m_LifeText;

    public void Awake()
    {
        m_MeshRenderer = GetComponentInChildren<MeshRenderer>();
        m_UnitAnimation = GetComponent<UnitAnimation>();
        m_AtkText = transform.GetChild(0).Find("info").Find("atkText").GetComponent<Text>();
        m_LifeText = transform.GetChild(0).Find("info").Find("lifeText").GetComponent<Text>();
    }
    public void Init(UnitBase unit)
    {
        m_Unit = unit;
        transform.localPosition = new Vector3(unit.Pos.x, 0, unit.Pos.y);
        m_MeshRenderer = GetComponentInChildren<MeshRenderer>();
        m_MeshRenderer.sortingOrder = 2 * (m_Unit.board.Height - m_Unit.Pos.y);
    }

    public void Refresh(Team viewer)
    {
        if (m_Unit.team == viewer || m_Unit.visible_to_opponent)
        {
            m_MeshRenderer.enabled = true;
            m_AtkText.enabled = true;
            m_LifeText.enabled = true;

            m_AtkText.text = Mathf.Max(m_Unit.atk + m_Unit.atk_fix, 0).ToString();
            if (m_Unit.atk_fix < 0)
            {
                m_AtkText.color = Color.red;
            }
            else if (m_Unit.atk_fix == 0)
            {
                m_AtkText.color = Color.white;
            }
            else
            {
                m_AtkText.color = Color.green;
            }

            m_LifeText.text = m_Unit.life.ToString();
            if (m_Unit.life < m_Unit.max_life)
            {
                m_LifeText.color = Color.red;
            }
            else if (m_Unit.life == m_Unit.max_life)
            {
                m_LifeText.color = Color.white;
            }
            else
            {
                m_LifeText.color = Color.green;
            }
        }
        else
        {
            m_MeshRenderer.enabled = false;
            m_AtkText.enabled = false;
            m_LifeText.enabled = false;
        }
    }
    public void RefreshInfo()
    {
        
    }

    public void OnStartAction()
    {
        if (m_Unit.team == m_BoardDisplay.m_ViewPoint)
        {
            m_UnitAnimation.SetCurrentActionHighlight(true);
        }
        else
        {
            m_UnitAnimation.SetOutline(true, Color.red);
        }
    }

    public void OnEndAction()
    {
        if (m_Unit.team == m_BoardDisplay.m_ViewPoint)
        {
            m_UnitAnimation.SetCurrentActionHighlight(false);
        }
        else
        {
            m_UnitAnimation.SetOutline(false, Color.red);
        }
    }

    public void OnMoveEnd()
    {

    }
    public void OnDieEnd()
    {

    }
    public void OnHurt()
    {

    }
    public void OnAttack()
    {

    }
    // 开始长按
    public void OnLongPress()
    {

    }
    // 取消长按
    public void OnCancelLongPress()
    {

    }
}